import mockAS2.IMockAS2;

/**
 * Copyright (c) 2012 Gary Higham
 * Released under the MIT license
 * https://code.google.com/p/mock-as2/wiki/License
 *
 * Version: v0.1
 */
class mockAS2.SpyObject implements IMockAS2 {
	
	private var callbackFuncWhenDeleted : Function;
	private var parentObject : Object;
	private var objectToWatch : String;
	private var valuesSet : Object;
		
	public function SpyObject(parentObject : Object, objectToWatch : String) {;
		parentObject.watch(objectToWatch, onObjectUpdated, null);
		this.parentObject = parentObject;
		this.objectToWatch = objectToWatch;
		valuesSet = new Object();
	}

	private function onObjectUpdated(object : Object, oldVal : Object, newVal : Object) : Object {
		valuesSet[newVal] = true;
		if(newVal == undefined) {
			return oldVal;
		}
		return newVal;
	}

	public function hasBeenDeleted() : Boolean {
		return getObject() == undefined;
	}
	
	public function hasBeenNulled() : Boolean {
		return valuesSet[null] == true;
	}
	
	public function restore() : Void {
		parentObject.unwatch(objectToWatch);
	}

	private function getObject() : Object {
		return Object(parentObject[objectToWatch]);
	}
}
